home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / drdobbs / 1987 / 08 / holub.exp < prev    next >
Text File  |  1987-08-12  |  6KB  |  167 lines

  1.  
  2.                    1:  call( first )
  3.                    2:  int   first;
  4.                    3:  {
  5.                    4:      int     *argp;
  6.                    5:  
  7.                    6:      argp = &first ;
  8.                    7:      while( *argp != 0 )
  9.                    8:              printf("%d\n", *argp++ );
  10.                    9:  }
  11.  
  12. Example 1: The call() subroutine 
  13.  
  14.  
  15.           01:  struct the_first
  16.           02:  {
  17.           03:      int      one;
  18.           04:      double   two;
  19.           05:  };
  20.           06:  
  21.           07:  struct the_second
  22.           08:  {
  23.           09:      double one;
  24.           10:      double two;
  25.           11:  };
  26.           12:  
  27.           13:  nannuck( struct_type, struct_itself )
  28.           14:  int     struct_type;
  29.           15:  {
  30.           16:      struct the_first  *firstp;
  31.           17:      struct the_second *secondp;
  32.           18:  
  33.           19:      if( struct_type == 1 )
  34.           20:      {
  35.           21:          firstp = (struct the_first *) &struct_itself;
  36.           22:          printf("%d, %f\n", firstp->one,
  37.           23:                             firstp->two );
  38.           24:      }
  39.           25:      else
  40.           26:      {
  41.           27:          secondp = (struct the_second *) &struct_itself;
  42.           28:          printf("%f, %f\n", secondp->one,
  43.           29:                             secondp->two );
  44.           30:      }
  45.           31:  }
  46.           32:  
  47.           33:  main()
  48.           34:  {
  49.           35:          nannuck(1,  10,    20.0 );
  50.           36:          nannuck(2,  30.0,  40.0 );
  51.           37:  }
  52.  
  53. Example 2:
  54.  
  55.  
  56.  
  57.         01:  #include <stdio.h>
  58.         02:  #define va_arg(argp,type) ((type *)(argp += sizeof(type)))[-1]
  59.         03:  
  60.         04:  fang( format, args )
  61.         05:  char    *format;
  62.         06:  {
  63.         07:      char        *argp = (char *) &args;
  64.         08:  
  65.         09:      for(;  *format ; format++ )
  66.         10:      {
  67.         11:          if( *format != '%' )
  68.         12:              putchar( *format );
  69.         13:          else
  70.         14:          {
  71.         15:              switch( *++format )
  72.         16:              {
  73.         17:              case 'c': printf("%c", va_arg(argp,int   ) ); break;
  74.         18:              case 'd': printf("%d", va_arg(argp,int   ) ); break;
  75.         19:              case 's': printf("%s", va_arg(argp,char *) ); break;
  76.         20:              case 'f': printf("%f", va_arg(argp,double) ); break;
  77.         21:              }
  78.         22:          }
  79.         23:      }
  80.         24:  }
  81.         25:  
  82.         26:  main()
  83.         27:  {
  84.         28:          fang("%c, %d, %s, %f\n", '1', 2, "3", 4.5 );
  85.         29:  }
  86.  
  87. Example 3:A printf()-like subroutine
  88.  
  89.  
  90.         01:  #include <stdarg.h>             /*     ANSI     */
  91.         02:  
  92.         03:  ansi_fang( format )
  93.         04:  char    *format;
  94.         05:  {
  95.         06:      va_list   argp;
  96.         07:      va_start( argp, format );
  97.         08:  
  98.         09:      for(;  *format ; format++ )
  99.         10:      {
  100.         11:          if( *format != '%' )
  101.         12:              putchar( *format );
  102.         13:          else
  103.         14:          {
  104.         15:              switch( *++format )
  105.         16:              {
  106.         17:              case 'c': printf("%c", va_arg(argp,int   ) ); break;
  107.         18:              case 'd': printf("%d", va_arg(argp,int   ) ); break;
  108.         19:              case 's': printf("%s", va_arg(argp,char *) ); break;
  109.         20:              case 'f': printf("%f", va_arg(argp,double) ); break;
  110.         21:              }
  111.         22:          }
  112.         23:      }
  113.         24:  }
  114.  
  115. Example 4: ANSI variable-argument conventions
  116.  
  117.  
  118.         01:  #include <varargs.h>            /*     UNIX     */
  119.         02:  
  120.         03:  unix_fang( va_alist )
  121.         04:  va_dcl                  /* NOTE: NO SEMICOLON PERMITTED HERE */
  122.         05:  {
  123.         06:      char      *format;
  124.         07:      va_list   argp;
  125.         08:      va_start( argp );
  126.         09:  
  127.         10:      for( format = va_arg(argp,char*); *format ; format++ )
  128.         11:      {
  129.         12:          if( *format != '%' )
  130.         13:              putchar( *format );
  131.         14:          else
  132.         15:          {
  133.         16:              switch( *++format )
  134.         17:              {
  135.         18:              case 'c': printf("%c", va_arg(argp,int   )); break;
  136.         19:              case 'd': printf("%d", va_arg(argp,int   )); break;
  137.         20:              case 's': printf("%s", va_arg(argp,char *)); break;
  138.         21:              case 'f': printf("%f", va_arg(argp,double)); break;
  139.         22:              }
  140.         23:          }
  141.         24:      }
  142.         25:  }
  143.  
  144. Example 5: Unix variable-argument conventions
  145.  
  146.         298:    else
  147.         299:    {
  148.                     if( sizeof(VTYPE) != sizeof(double) )
  149.                          rval = (VTYPE) atol( Str );
  150.                     else
  151.                     {
  152.                         rval = atof( Str );
  153.  
  154.                         while( isdigit(*Str) || *Str == '.' )
  155.                             Str++;
  156.  
  157.                         if(*Str == 'E' || *Str == 'e' )     /* 12.34E+03 */
  158.                             Str += 2 ;
  159.                     }
  160.  
  161.                     while( isdigit(*Str) )
  162.                             Str++;
  163.         307:    }
  164.  
  165. Example 6: Corrections to code in C Chest, Listing Six, February 1987 
  166.  
  167.